package com.xkdx.caipiao.presistence.findpsw;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;

public class FindPwsCodeActivity extends ThreadActivity {

	IdentifyCodeAction action;
	IdentifyCodeModule module;
	IdentifyCodePresisence presisence;
	EditText et_vcode;
	Button send, next;
	TextView tv_phone;
	public static String vcode;

	FindPwsAction findPwsAction;
	FindPswModule findPswModule;
	FindPswPresistence findPswPresistence;
	boolean isSend = false;
	private TextView textView1;
	private int remainingTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_psw2_caipiao);
		tv_phone = (TextView) findViewById(R.id.phone);
		tv_phone.setText("你绑定的手机号为：" + FindPwsActivity.user_name);
		et_vcode = (EditText) findViewById(R.id.code);
		textView1 = (TextView) findViewById(R.id.textView1);
		send = (Button) findViewById(R.id.send_vcode);
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				isSend = true;
				startSendThread();

			}
		});

		next = (Button) findViewById(R.id.next);
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				vcode = et_vcode.getText().toString();
				if (vcode.equals("")) {
					toast("请输入验证码");
				} else {
					startThread();
				}

			}
		});
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (isSend) {
			if (result.containsKey("0")) {
				isSend = false;
				if (findPswModule.isSuccess) {
					toast("已发送验证码");
					remainingTime = 60;
					InitTimer();
				} else {
					toast(findPswModule.message);
				}
			} else {
				toastErrorMessage(result);
			}
			return;
		}

		if (result.containsKey("0")) {
			if (module.isSuccess) {
				Intent intent = new Intent(this, ReSetPswActivity.class);
				startActivity(intent);
			} else {
				toast(module.Message);
			}
		} else {
			toastErrorMessage(result);
		}
	}

	Timer timer;

	/**
	 * 验证码时间
	 */
	private void InitTimer() {

		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				if (remainingTime > 0) {
					nextHandler.sendEmptyMessage(1);
					remainingTime--;
				} else {
					nextHandler.sendEmptyMessage(2);

				}

			}
		}, 0, 1000);

	}

	Handler nextHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				textView1.setVisibility(View.VISIBLE);
				textView1.setText("还剩" + remainingTime + "秒验证码过期");
				break;
			case 2:
				if (timer != null) {
					timer.cancel();
					timer = null;
				}
				textView1.setText("请获取验证码");
				break;
			default:
				break;
			}

		}
	};

	private void startSendThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		findPwsAction = new FindPwsAction(FindPwsActivity.user_name);
		findPswModule = new FindPswModule();
		findPswPresistence = new FindPswPresistence(this);
		findPswPresistence.setActitons(findPwsAction);
		findPswPresistence.setModule(findPswModule);
		findPswPresistence.execute();
	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		action = new IdentifyCodeAction(FindPwsActivity.user_name, vcode);
		module = new IdentifyCodeModule();
		presisence = new IdentifyCodePresisence(this);
		presisence.setActitons(action);
		presisence.setModule(module);
		presisence.execute();

	}

	@Override
	public void onBackPressed() {

		super.onBackPressed();
	}

	@Override
	public void onBackClick(View view) {
		finish();

	}

}
